package com.bgt.springboot.first.controller;

import com.bgt.springboot.first.mapper.UserMapper;
import com.bgt.springboot.first.model.User;
import com.bgt.springboot.first.repository.FoodRepository;
import com.bgt.springboot.first.repository.UserPageRepository;
import com.bgt.springboot.first.repository.UserUuidRepository;
import com.bgt.springboot.first.service.UserService;
import com.bgt.springboot.first.vo.Food;
import com.bgt.springboot.first.vo.UserUuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by bgt on 2017/4/18.
 * thymeleaf要求html必须闭合
 */
@Controller
public class IndexController {
    @Autowired
    private FoodRepository foodRepository;
    @Autowired
    private UserUuidRepository userUuidRepository;
    @Autowired
    private UserPageRepository userPageRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    /**
     * thymeleaf响应的页面
     * @param map
     * @return
     */
    @RequestMapping("/index")
    @ResponseBody
    public Page<com.bgt.springboot.first.vo.User> index(ModelMap map) {
        map.put("name","bgt");
        //(new TeacherSpecification()).setparams(null);
        //Page<com.bgt.springboot.first.vo.User> page2=new Page<com.bgt.springboot.first.vo.User>(1);
        //page2=userService.findAll(page2);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(0, 10, sort);
        Page<com.bgt.springboot.first.vo.User> page=userPageRepository.findByAge(101,pageable);
        return page;
    }

    /**
     * jsp响应的页面
     * @param map
     * @return
     */
    @RequestMapping("/index2")
    public String index2(ModelMap map) {
        map.put("name2","bgt2");
        //jpa查询
        List<Food> foodList=foodRepository.findByCategoryId(1);
        System.out.println(foodList.get(0).getFoodCategory().getName());
        //mybatis
        List<User> users=userMapper.findByName(100);
        System.out.println("mybatis--->users.size():"+users.size());
        UserUuid userUuid=new UserUuid();
        userUuid.setAge(10);
        userUuidRepository.save(userUuid);
        return "index2";
    }


}
